﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SharedCode;
using System.Collections.ObjectModel;
using AdminModule.IndexMarkServiceReference;
using System.Linq;

namespace AdminModule.ViewModels
{
    public class EditResponsiblesVM : ViewModelBase
    {
        private IndexMarkServiceClient client = new IndexMarkServiceClient();

        public EditResponsiblesVM()
        {
            client.GetAdmPositionsCompleted += new EventHandler<GetAdmPositionsCompletedEventArgs>(client_GetAdmPositionsCompleted);
            client.GetAdmDepartmentsCompleted += new EventHandler<GetAdmDepartmentsCompletedEventArgs>(client_GetAdmDepartmentsCompleted);
            this.LoadCompleted += new LoadEventHandler(EditResponsiblesVM_LoadCompleted);
        }

        public override void Init()
        {
            base.Init();
            this.LoadAdmPosition();
        }

        void EditResponsiblesVM_LoadCompleted(object sender, LoadEventArgs e)
        {
            if (e.Type == typeof(AdmPosition))
            {
                client.GetAdmDepartmentsAsync(AdmDepartmentSortMode.FullName);
            }
            if (e.Type == typeof(AdmDepartment))
            {
                this.RaiseInitCompleted();
            }
        }

        #region Должности
        private ObservableCollection<AdmPosition> positions = new ObservableCollection<AdmPosition>();
        public ObservableCollection<AdmPosition> AdmPositionCollection
        {
            get
            {
                return this.positions;
            }
            set
            {
                this.positions = value;
            }
        }

        void client_GetAdmPositionsCompleted(object sender, GetAdmPositionsCompletedEventArgs e)
        {
            positions.Clear();
            foreach (var s in e.Result)
            {
                positions.Add(s);
            }
            this.LoadCompletedInit(typeof(AdmPosition));
        }

        public void LoadAdmPosition()
        {
            client.GetAdmPositionsAsync();
        }
        #endregion

        #region Отеделы
        private ObservableCollection<AdmDepartment> departments = new ObservableCollection<AdmDepartment>();
        public ObservableCollection<AdmDepartment> AdmDepartmentCollection
        {
            get
            {
                return this.departments;
            }
            set
            {
                this.departments = value;
            }
        }

        void client_GetAdmDepartmentsCompleted(object sender, GetAdmDepartmentsCompletedEventArgs e)
        {
            departments.Clear();
            departments.Add(null);
            foreach (var s in e.Result)
            {
                departments.Add(s);
            }
            this.LoadCompletedInit(typeof(AdmDepartment));
        }
        #endregion
    }
}
